﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Threading;
using DoubleH.Utility.Configuration;
using Table=FCNS.Data.Table;
using System.Windows.Media;
using DoubleH.Utility;
using System.Collections.ObjectModel;
using POS.Plugins;

namespace POS
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        static PluginManager pluginManager = new PluginManager();
        /// <summary>
        /// 模板管理器
        /// </summary>
        public static PluginManager PluginManager { get { return pluginManager; } }
        /// <summary>
        /// 用于验证 强制交班
        /// </summary>
        public static string[] ForceJiaoBan { get; set; }

        static Brush backgroundBrush = Brushes.Black;
        public static Brush BackgroundBrush
        {
            get { return backgroundBrush; }
        }

        static Brush foregroundBrush = Brushes.White;
        public static Brush ForegroundBrush
        {
            get { return foregroundBrush; }
        }

        static bool labelSaleMode = true;
        /// <summary>
        /// 销售模式还是练习模式
        /// </summary>
        public static bool SaleMode
        {
            get { return labelSaleMode; }
            set
            {
                labelSaleMode = value;
                MWindow.labelSaleMode.Content = value ? "零售模式" : "练习模式";
            }
        }
        /// <summary>
        /// 当前班次
        /// </summary>
        public static Table.PosShiftS CurrentShift { get; set; }

        static Table.PosOrderS order = null;
        /// <summary>
        /// 当前操作中的订单
        /// </summary>
        public static Table.PosOrderS Order
        {
            get { return order; }
            set
            {
                order = value;
            }
        }

        static ObservableCollection<ClassGuaDan> guaDan = new ObservableCollection<ClassGuaDan>();
        /// <summary>
        /// 挂单列表
        /// </summary>
        public static ObservableCollection<ClassGuaDan> GuaDan
        {
            get { return guaDan; }
            set { guaDan = value; }
        }
        /// <summary>
        /// 当前POS机的实例
        /// </summary>
        public static Table.PosS Pos
        {
            get;
            set;
        }

        static MainWindow main = null;
        public static MainWindow MWindow
        {
            get { return main; }
        }

        internal void InitDatabase()
        {
            //初始化数据库
            //if (DoubleHConfig.AppConfig.SingleMode)
            //{
            //    FCNS.Data.SQLdata.SqlConfig = new FCNS.Data.SQLdata.SqlString()
            //    {
            //        DataType = FCNS.Data.DataType.SQLITE,
            //        Db = FCNS.Data.DbDefine.dbFile
            //    };
            //}
            //else
            //{
            DataConfig dc = DoubleHConfig.AppConfig.DataConfigItems.First(f => { return f.Flag == DoubleHConfig.AppConfig.DataFlag; });
            FCNS.Data.SQLdata.SqlConfig = new FCNS.Data.SQLdata.SqlString()
            {
                DataType = (FCNS.Data.DataType)Enum.Parse(typeof(FCNS.Data.DataType), dc.DataType),
                Db = dc.DataName,
                IPorName = dc.DataAddress,
                Password = dc.DataPassword,
                User = dc.DataUser
            };
            //}

            if (!Table.SysConfig.SysConfigParams.UsePos)
            {
                MessageWindow.Show("POS零售 未启用");
                Environment.Exit(0);
            }
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            InitDatabase();//如果不在这里加载,args 参数中如果涉及数据库的调用就会出错
            if (RunArgs(e))
                Environment.Exit(0);

            base.OnStartup(e);

            bool bRun = true;
            Mutex m = new Mutex(true, "POS.exe", out bRun);
            if (bRun)
            {
                backgroundBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString(DoubleHConfig.AppConfig.PosBackgroundColor));
                foregroundBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString(DoubleHConfig.AppConfig.PosForegroundColor));

                m.ReleaseMutex();
                main = new MainWindow();
                main.Show();
            }
            else
                Application.Current.Shutdown();
        }

        private bool RunArgs(StartupEventArgs e)
        {
            if (e.Args != null && e.Args.Length > 0)
            {
                switch (e.Args.Length)
                {
                    case 1:
                        if (e.Args[0] == "?")
                            MessageWindow.Show("命令行参数帮助", "[-p] [可销售商品Id] 编辑禁售商品POS列表/r/s" +
                                                                "[-u] [POS机Id] 编辑POS收银员列表/r/s");

                        break;
                    case 2:
                            if (e.Args[0] == "p" || e.Args[0] == "u")
                            {
                                WindowStopSale wss = new WindowStopSale();
                                wss.Init(e.Args[0],e.Args[1]);
                                wss.ShowDialog();
                            }
                        break;
                }

                return true;
            }
            else
                return false;
        }
    }
}